package com.digitaldiary.bean;

import com.digitaldiary.entity.MyPlan;
import java.util.List;
import javax.annotation.Resource;
import javax.el.ValueExpression;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIParameter;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
import javax.persistence.Query;
import javax.transaction.UserTransaction;

/**
 *
 * @author palash
 */
@ManagedBean(name = "myPlanHandler")
@SessionScoped
public class MyPlanHandler {
    @PersistenceUnit
    private EntityManagerFactory entityManagerFactory;
    @Resource
    private UserTransaction myplanTransaction;
    
    @ManagedProperty(value="#{homeHandler}")
    private HomeHandler loguser ;

    private MyPlan myPlan;
     private MyPlan selectedPlan;
    
    public HomeHandler getLoguser() {
        return loguser;
    }

    public void setLoguser(HomeHandler loguser) {
        this.loguser = loguser;
    }

    
    public MyPlanHandler(){
    
    }
    
    public MyPlan getMyPlan() {
        return myPlan;
    }

    public void setMyPlan(MyPlan myPlan) {
        this.myPlan = myPlan;
    }
  
    
  
    public void saveMyPlan() {
        EntityManager entityManager = entityManagerFactory.createEntityManager();
        //String username= FacesContext.getCurrentInstance().getExternalContext().
          //      getRequestParameterMap().get("hidden");
        String name= loguser.currentLoggedInUser.getUserName();
            
        try {
            MyPlan plan = new MyPlan();
            plan.setUsername(name);
            plan.setTitle(this.title);
            plan.setDescription(this.description);
            plan.setLocation(this.location);
            plan.setPlandate(plandate);
            plan.setPrivacy(privacy);
            plan.setAlarmDate(this.alarmDate);

            myplanTransaction.begin();
            entityManager.persist(plan);
            myplanTransaction.commit();
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }
    
   
    public List<MyPlan> getMyPlanList(){
        EntityManager entityManager= entityManagerFactory.createEntityManager();
         List<MyPlan> planList=null;
          String name= loguser.getLoggedInUser().getUserName();
        try{
           
            System.out.println("Hellolllll");
            Query query= entityManager.createQuery("Select m from MyPlan m where m.username=:name");
            query.setParameter("name", name);
            planList=  query.getResultList();        
        }catch(Throwable e){
             e.printStackTrace();
        }
       return planList;
    }

   
    public void removePlan(long id){
        EntityManager entityManager = entityManagerFactory.createEntityManager();
        try{
          
            myplanTransaction.begin();
            Query query= entityManager.createQuery("DELETE FROM MyPlan p WHERE p.id=:id");
            int deleted=query.setParameter("id",id).executeUpdate();
            myplanTransaction.commit();
            
            System.out.println(" working ");
            
        }catch(Throwable e){
            e.printStackTrace();
        }
       
    }
    
    public void updatePlan(){
        System.out.println(selectedPlan.getId());
        EntityManager em= entityManagerFactory.createEntityManager();
       
        try{
            
            myplanTransaction.begin();
            Query query= em.createQuery("UPDATE MyPlan p "
                    + "SET p.title=:title, p.description=:description,"
                    +"p.location=:location, p.plandate=:plandate, p.privacy=:privacy, p.alarmDate=:alarmDate"
                    + " WHERE p.id=:id");
            query.setParameter("id", selectedPlan.getId());
            query.setParameter("title", selectedPlan.getTitle());
            query.setParameter("description", selectedPlan.getDescription());
            query.setParameter("location", selectedPlan.getLocation());
            query.setParameter("plandate", selectedPlan.getPlandate());
            query.setParameter("privacy", selectedPlan.getPrivacy());
            query.setParameter("alarmDate", selectedPlan.getAlarmDate());
            query.executeUpdate();
            System.out.println("Enter in update");
            myplanTransaction.commit();
            
        }catch(Throwable e){
         e.printStackTrace();
        }
    }

    public MyPlan getSelectedPlan() {
        return selectedPlan;
    }

    public void setSelectedPlan(MyPlan selectedPlan) {
        this.selectedPlan = selectedPlan;
    }

   
    
    private Long id;
   
    private String username;
    private String title;
    private String description;
    private String location;
    private String plandate;
    private String privacy;
    private String alarmDate;
    
      public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public EntityManagerFactory getEntityManagerFactory() {
        return entityManagerFactory;
    }

    public void setEntityManagerFactory(EntityManagerFactory entityManagerFactory) {
        this.entityManagerFactory = entityManagerFactory;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public UserTransaction getMyplanTransaction() {
        return myplanTransaction;
    }

    public void setMyplanTransaction(UserTransaction myplanTransaction) {
        this.myplanTransaction = myplanTransaction;
    }

    public String getPlandate() {
        return plandate;
    }

    public void setPlandate(String plandate) {
        this.plandate = plandate;
    }

    public String getPrivacy() {
        return privacy;
    }

    public void setPrivacy(String privacy) {
        this.privacy = privacy;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getAlarmDate() {
        return alarmDate;
    }

    public void setAlarmDate(String alarmDate) {
        this.alarmDate = alarmDate;
    }
    
    
}
